package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class MenuSerivceImpl implements MenuSerivce {

    @Autowired
    MenuDao menuDao;


    @Override
    public void add(Menu menu) {
        Menu menuDB = menuDao.findByName(menu.getName());
        if (menuDB != null) {
            throw new BusinessRuntimeException("该菜单已存在,请重新添加");
        }
        menuDao.add(menu);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<Menu> page = menuDao.findByPage(queryPageBean.getQueryString());

        long total = page.getTotal();


        return new PageResult(total,page);
    }

    @Override
    public Menu findById(Integer id) {

        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delById(Integer id) {
        Long count = menuDao.findCountById(id);
        if (count > 0) {
            throw new BusinessRuntimeException("该菜单被关联,不能删除!!!");
        }

        menuDao.delById(id);
    }
}
